﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScheduleMaker.DB;
using ScheduleMaker.DB.CoreDAO;

namespace ScheduleMaker
{
    public partial class smAdmEditOvergroupSubjects : Form
    {
        IList<DB.Domain.Core.Subject> subjects;
        DB.Domain.Core.Category category;

        public smAdmEditOvergroupSubjects(DB.Domain.Core.Category _category)
        {
            InitializeComponent();
            this.category = _category;

            //Console.WriteLine(this + " " + category);
            if( category == null) Console.WriteLine("Category - NULLLL ");

            this.getSubjectList();
            this.getOvergroupSubjectList();
            
        }

        private void bAddOvergroupSubject_Click(object sender, EventArgs e)
        {

            int selectedIndex = lbSubjects.SelectedIndex;
            try
            {
                DB.Domain.Core.Subject subject = subjects.ElementAt(selectedIndex);

                if (!category.Subjects.Contains(subject))
                {
                    CategoryDAO categoryDAO = DAOFactory.getCategoryDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                    category.Subjects.Add(subject);
                    categoryDAO.saveOrUpdate(category);
                    MessageBox.Show("Przedmiot został dodany", "Dodawanie przedmiotów" + selectedIndex);
                }
                else
                {
                    MessageBox.Show("Przedmiot już dodano!", "Błąd " + selectedIndex);
                }
            }
            catch
            {
                MessageBox.Show("Nie wybrano przedmiotu!", "Błąd " + selectedIndex);
            }
            this.getOvergroupSubjectList();
        }


        // Pobieranie - przedmiotów
        private void getSubjectList()
        {
            lbSubjects.Items.Clear();
            SubjectDAO subjectDAO = DAOFactory.getSubjectDAO(ConfigManager.ConfigManager.getConfiguration().Type);
            subjects = subjectDAO.getAll();
            foreach (var elem in subjects)
            {
                lbSubjects.Items.Add(elem.Id + ": " + elem.Name);
                //Console.WriteLine("User id: " + elem.Id + " Login: " + elem.Login);
            }
        }

        private void getOvergroupSubjectList()
        {
            lbOverGroupSubjects.Items.Clear();

                foreach (var elem in this.category.Subjects)
                {
                    lbOverGroupSubjects.Items.Add(elem.Id + ": " + elem.Name);
                }

        }


        private void bRemOvergroupSubject_Click(object sender, EventArgs e)
        {
            // coś tam xD - gdy usuwamy
            int selectedIndex = lbOverGroupSubjects.SelectedIndex;
            try
            {
                if (MessageBox.Show("Czy na pewno chcesz usunąć ten przedmiot?", "Potwierdź usunięcie", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //DB.Domain.Core.Category category = 
                    DB.Domain.Core.Subject subject = category.Subjects.ElementAt(selectedIndex);

                    CategoryDAO categoryDAO = DAOFactory.getCategoryDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                    category.Subjects.Remove(subject);
                    categoryDAO.saveOrUpdate(category);

                    // odświeżenie listy
                    this.getOvergroupSubjectList();
                }
            }
            catch
            {
                MessageBox.Show("Nie wybrano przedmiotu!", "Błąd " + selectedIndex);
            }
        }
    }
}
